package com.example.crm.service.impl;

import com.example.crm.mapper.CustomerMapper;
import com.example.crm.pojo.Customer;
import com.example.crm.service.ICustomerService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class CustomerServicceImpl implements ICustomerService
{

    CustomerMapper customerMapper;

    SqlSessionFactory sqlSessionFactory;

    {
        // 1. 创建SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

        // 2. 加载SqlMapConfig.xml配置文件
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 3. 创建SqlSessionFactory对象
        this.sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);

        SqlSession sqlSession = this.sqlSessionFactory.openSession();

        customerMapper = sqlSession.getMapper(CustomerMapper.class);
    }

    @Override
    public List<Customer> findAll()
    {
        return customerMapper.findAll();
    }4

    @Override
    public int deleteCustomer(int id) {
        int i = customerMapper.deleteCustomer(id);
        return i;
    }

    @Override
    public int addCustomer(Customer customer) {
        int i = customerMapper.addCustomer(customer);
        return i;
    }
}
